#!/usr/bin/env python

#
# OpenLookup - config.py
#
# Copyright 2008-2009 Helsinki Institute for Information Technology
# and the authors.
#
# Authors: Ken Rimey <rimey@hiit.fi>
#

# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation files
# (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge,
# publish, distribute, sublicense, and/or sell copies of the Software,
# and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

from openlookup.engine.db import DB, FILENAME
from openlookup.server.sync import SYNC_PORT, RATE_LIMIT, CONNECTION_LIMIT

def add_peer(db, host, port=SYNC_PORT):
    address = host, port
    peers = db.get_info('peers', [])
    if address not in peers:
        peers.append(address)
        db.set_info('peers', peers)

def clear_peers(db):
    db.clear_info('peers')

def print_config(db):
    print

    name = db.get_info('name', None)
    if name is None:
        print 'No name defined.'
    else:
        print 'Name:', name

    contact = db.get_info('contact', None)
    if contact is not None:
        print 'Contact:', contact

    rate_limit = db.get_info('rate_limit', None)
    if rate_limit is not None:
        print 'Rate limit:', rate_limit

    connection_limit = db.get_info('connection_limit', None)
    if connection_limit is not None:
        print 'Connection limit:', connection_limit

    forward = db.get_info('forward', None)
    if forward is not None:
        print 'Forward:', forward

    print
    addresses = db.get_info('peers', [])
    if not addresses:
        print 'No peers defined.'
    else:
        print 'Peer addresses:'
        for address in addresses:
            print '  %s %s' % address

if __name__ == '__main__':
    from optparse import OptionParser

    parser = OptionParser()

    parser.add_option('--db', dest='filename', default=FILENAME,
                      help='location of database (default is %s)' % FILENAME)
    parser.add_option('--name', default=None,
                      help='assign a name to this node')
    parser.add_option('--contact', default=None,
                      help='e-mail or other contact information for node admin')
    parser.add_option('--add', dest='host', default=None,
                      help='add a peer to connect to')
    parser.add_option('--port', type='int', default=SYNC_PORT,
                      help='port to connect to at peer (default is %s)' % SYNC_PORT)
    parser.add_option('--clear', action='store_true', default=False,
                      help='clear list of peer addresses')
    parser.add_option('--rate_limit', type='int', default=None,
                      help='bytes per second in for new connections'
                           ' (default is %s)' % RATE_LIMIT)
    parser.add_option('--connection_limit', type='int', default=None,
                      help='number of connections'
                           ' after which new incoming connections are rejected'
                           ' (default is %s)' % CONNECTION_LIMIT)
    parser.add_option('--enable_forwarding', dest='forward',
                      action='store_true', default=None,
                      help="forward data received from peers (default)")
    parser.add_option('--disable_forwarding', dest='forward',
                      action='store_false', default=None,
                      help="don't forward data received from peers")

    options, args = parser.parse_args()
    if args:
        parser.error('incorrect number of arguments')

    report = True

    db = DB(options.filename)

    if options.clear:
        clear_peers(db)
        report = False

    if options.host is not None:
        add_peer(db, options.host, options.port)
        report = False

    if options.name is not None:
        db.set_info('name', options.name)
        report = False

    if options.contact is not None:
        db.set_info('contact', options.contact)
        report = False

    if options.rate_limit is not None:
        db.set_info('rate_limit', options.rate_limit)
        report = False

    if options.connection_limit is not None:
        db.set_info('connection_limit', options.connection_limit)
        report = False

    if options.forward is not None:
        db.set_info('forward', options.forward)
        report = False

    if report:
        print_config(db)
